package com.mindcraft.chat.controller;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.base.utils.CommonConst;
import com.mindcraft.base.utils.JwtHelper;
import com.mindcraft.chat.po.ImChatUserFriend;
import com.mindcraft.chat.po.ImChatUserMessage;
import com.mindcraft.chat.service.ImChatUserFriendService;
import com.mindcraft.chat.service.ImChatUserMessageService;
import com.mindcraft.chat.utils.LoginCheck;
import com.mindcraft.chat.vo.MessageRequestVo;
import com.mindcraft.chat.vo.NewsListVo;
import com.mindcraft.chat.vo.SystemMessageVo;
import com.mindcraft.chat.vo.UserFriendVO;
import com.mindcraft.user.po.User;
import com.mindcraft.user.utils.MindCraftUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Api(value = "shop", tags = "商城消息接口")
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ImChatUserFriendService userFriendService;

    @Autowired
    private ImChatUserMessageService imChatUserMessageService;

    /**
     * 查询好友
     */
    @PostMapping("/getFriend")
    @LoginCheck
    @ApiOperation("根据用户id查询用户的好友")
    public MindCraftResult<List<UserFriendVO>> getFriend() {

        String token = MindCraftUtil.getRequest().getHeader(CommonConst.TOKEN);
        String userId = JwtHelper.getUserId(token);

        return MindCraftResult.success(userFriendService.getFriend(userId));

    }

    /**
     * 添加好友申请
     */
    @PostMapping("/addFriend")
    @LoginCheck
    @ApiOperation("添加好友")
    public MindCraftResult addFriend(@RequestBody ImChatUserFriend imChatUserFriend) {

        String token = MindCraftUtil.getRequest().getHeader(CommonConst.TOKEN);
        String userId = JwtHelper.getUserId(token);
        imChatUserFriend.setUserId(userId);

        return userFriendService.addFriend(imChatUserFriend);


    }

    /**
     * 添加好友申请
     */
    @PostMapping("/findNewsList")
    @LoginCheck
    @ApiOperation("获取消息列表")
    public MindCraftResult findNewsList(@RequestBody NewsListVo newsListVo) {

        String token = MindCraftUtil.getRequest().getHeader(CommonConst.TOKEN);
        String userId = JwtHelper.getUserId(token);

         newsListVo.setUserId(userId);
        return userFriendService.findNewsList(newsListVo);


    }



    /**
     * 获取朋友消息
     */
    @PostMapping("/listFriendMessage")
    @LoginCheck
    @ApiOperation("获取朋友消息")
    public MindCraftResult listFriendMessage(@RequestBody MessageRequestVo messageRequestVo) {


        //获取当前用户
        String token = MindCraftUtil.getRequest().getHeader(CommonConst.TOKEN);
        String userId = JwtHelper.getUserId(token);
        messageRequestVo.setUserId(userId);

        return imChatUserMessageService.listFriendMessage(messageRequestVo);
    }




    /**
     * 获取系统消息（只获取前十条）
     */
    @PostMapping("/listSystemMessage")
    @LoginCheck
    @ApiOperation("获取系统消息")
    public MindCraftResult listSystemMessage(@RequestBody SystemMessageVo systemMessageVo) {

        return imChatUserMessageService.listSystemMessage(systemMessageVo);



    }


}
